﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_Public;
using QLT_BLL;

namespace GiaoDien
{
    public partial class uc_QuyenHan : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_QuyenHan()
        {
            InitializeComponent();
        }
        PhanQuyen_BLL phanQuyen_bll = new PhanQuyen_BLL();

        private void uc_QuyenHan_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = phanQuyen_bll.LayDuLieu();
            groupControl1.Hide();
            bbi_Huy.Enabled = false;
            bbi_Luu.Enabled =false;
        }
        int flag;
        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 0;
            txtMaQuyen.Enabled = false;
            bbi_Xoa.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            groupControl1.Show();
        }
        PhanQuyen_Public pq = new PhanQuyen_Public();
        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 1;
            bbi_Xoa.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            gridControl1_DoubleClick(sender,e);
            errorProvider1.Clear();
            groupControl1.Show();
        }



        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtTenQuyen.Text != "" && txt_GhiChu.Text != "")
            {
                if (flag == 0)
                {
                    //them
                    pq.TenQuyen = txtTenQuyen.Text;
                    pq.GhiChu = txt_GhiChu.Text;
                    phanQuyen_bll.insert_PhanQuyen(pq, pq);
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if (flag == 1)
                {
                    //sua
                    pq.MaPQ = int.Parse(txtMaQuyen.Text);
                    pq.TenQuyen = txtTenQuyen.Text;
                    pq.GhiChu = txt_GhiChu.Text;
                    phanQuyen_bll.update_PhanQuyen(pq, pq, pq);
                    XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                uc_QuyenHan_Load(sender, e);
                groupControl1.Show();
            }

            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                pq.MaPQ = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Phân Quyền").ToString());
                pq.TenQuyen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
                if (XtraMessageBox.Show("Bạn có muốn xóa Quyền: '" + pq.TenQuyen + "' có Mã là: '" + pq.MaPQ + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    phanQuyen_bll.delete_PhanQuyen(pq);
                    uc_QuyenHan_Load(sender, e);
                }
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Không thể xóa do có bản ghi phụ thuộc!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Phân Quyền").ToString();
            txtTenQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            errorProvider1.Clear();
            groupControl1.Show();
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            txtMaQuyen.ResetText();
            txtTenQuyen.ResetText();
            txt_GhiChu.ResetText();
            bbi_Them.Enabled =true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            bbi_Xoa.Enabled =true;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            groupControl1.Hide();
        }

        private void bbi_Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            uc_QuyenHan_Load(sender,e);
        }

        private void txtTenQuyen_Leave(object sender, EventArgs e)
        {
            if (txtTenQuyen.Text == "")
                txtTenQuyen.Focus();
        }

        private void txt_GhiChu_Leave(object sender, EventArgs e)
        {
            if (txt_GhiChu.Text == "")
                txt_GhiChu.Focus();
        }
        Thuoc_BLL thuoc_bll = new Thuoc_BLL();
        private void txtTenQuyen_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_PhanQuyen where  TenQuyen like N'" + txtTenQuyen.Text.Trim() + "'";
            DataTable dt = new DataTable();
            dt = thuoc_bll.taoBang(sql);
            if (dt.Rows.Count > 0)
            {
                errorProvider1.SetError(txtTenQuyen, "Tên Quyền này đã tồn tại!");
                txtTenQuyen.Focus();
            }
            else
                errorProvider1.Clear();
        }

        private void txtMaQuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
    }
}
